package com.fox.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fox.hiring.common.util.AppUtil;
import com.fox.hiring.company.dao.CompanyDao;
import com.fox.hiring.company.dao.PositionDao;
import com.fox.hiring.company.entity.CompanyEntity;
import com.fox.hiring.company.entity.PositionEntity;
import com.fox.hiring.talent.entity.ResumeEntity;

/**
 * Company Servlet
 * 
 * @author fox.
 * @since 2016年2月26日
 */
public class CompanyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private CompanyDao companyDao = new CompanyDao();
	private PositionDao positionDao = new PositionDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置POST请求的编码
		request.setCharacterEncoding("UTF-8");
		// 根据命令参数，决定调用哪个操作方法
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else {
			throw new RuntimeException("请求失败！");
		}
	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取参数；
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		// 2、调用业务逻辑；
		CompanyEntity company = companyDao.login(code, password);
		// 3、转向
		if (company == null) {
			response.sendRedirect("view/company/login.jsp?loginError=true");
		} else {
			request.getSession().setAttribute("company", company);
			response.sendRedirect("CompanyServlet?command=main");
		}
	}

	/**
	 * 携带显示信息转到企业主页
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//查询当前登录者的所有职位发布信息
		int companyId = AppUtil.getCompanyId(request, response);
		List<PositionEntity> positionList = positionDao.list(companyId);
		request.setAttribute("positionList", positionList);

		
		// 查询当前登录企业所发布的职位
		request.getRequestDispatcher("view/company/main.jsp").forward(request,
				response);
	}

	/**
	 * 注销
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().invalidate();
		response.sendRedirect("view/company/login.jsp?sessionInvalidate=true");
	}
}
